<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/12
 * Time: 10:19
 */
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Db;
class Base extends Controller
{
    /**
     * 初始化操作
     */
    protected function _initialize()
    {
        //继承父类的初始化操作
        parent::_initialize();

        //系统参数
        $Config = Config();
        if ($Config['web_switch'] == 0){
            $this->redirect('Open/WebOff');
        }
        //获取登录Session
        $UserID = Session::get('user_id');
        $UserInfo = UserInfo(['id'=>$UserID]);
        if(empty($UserInfo)){
            Session::delete('user_id');
            //跳转登录界面
            $this->redirect('Open/login');
        }
        if(empty($UserID) || $UserID==""){
            $this->redirect('Open/Login');
        }
        if($UserInfo['status'] == 0){
            echo '<h1 style="text-align:center">当前账号未激活!</h1>';die;
        }
        $this->assign('title',$Config['web_name']);
    }
    
    //升级
    public function uplevel(){
        //获取升级条件配置
        $levelup = Db::name('user_level')->select();
        // var_dump($levelup);die;
        //获取当前用户id
        $UserID = Session::get('user_id');
        $where['user_id'] = $UserID;
        $where['recommend_id'] = [['neq', 1],['neq', $UserID]];
        //升级时  将所有上级id取出   自己和admin除外
        $ids= Db::name('user_recommend')->where($where)->column('recommend_id');
        //遍历处理
        foreach ($ids as $v){
            //获取信息
            $info = UserInfo(['id'=>$v]);
            //会员->一星
            if($info['level'] == 2){
                if($info['zhi'] >= $levelup[2]['number'] && $info['team_num'] >= $levelup[2]['teamall']){
                    $upda['level'] = 3;
                    Db::name('user')->where(['id'=>$info['id']])->update($upda);
                }
            }
            //一星->二星
            if($info['level'] == 3){
                if($info['zhi'] >= $levelup[3]['number'] && $info['team_num'] >= $levelup[3]['teamall']){
                    $upda['level'] = 4;
                    Db::name('user')->where(['id'=>$info['id']])->update($upda);
                }
            }
            //二星->三星
            if($info['level'] == 4){
                if($info['zhi'] >= $levelup[4]['number'] && $info['team_num'] >= $levelup[4]['teamall']){
                    $upda['level'] = 5;
                    Db::name('user')->where(['id'=>$info['id']])->update($upda);
                }
            }
            //三星->四星  升四星要注意县代,市代,省代升级
            if($info['level'] == 5){
                if($info['zhi'] >= $levelup[5]['number'] && $info['team_num'] >= $levelup[5]['teamall']){
                    $upda['level'] = 6;
                    Db::name('user')->where(['id'=>$info['id']])->update($upda);
                    //升4星时  将自己添加到上级的升级条件中  上级的等级大于等于四星等级
                    $upinfo = UserInfo(['id'=>$info['recommend']]);
                    if($upinfo['level'] >= 6){
                        $heigh['sixing'] = $upinfo['sixing'] + 1;
                        Db::name('user')->where(['id'=>$upinfo['id']])->update($heigh);
                    }
                }
            }
            //四星->县代
            if($info['level'] == 6){
                if($info['sixing'] >= $levelup[6]['number']){
                    $upda['level'] = 7;
                    Db::name('user')->where(['id'=>$info['id']])->update($upda);
                }
            }
            //县代->市代
            if($info['level'] == 7){
                if($info['sixing'] >= $levelup[7]['number']){
                    $upda['level'] = 8;
                    Db::name('user')->where(['id'=>$info['id']])->update($upda);
                }
            }
            //市代->省代  升省代要注意董事升级
            if($info['level'] == 8){
                if($info['sixing'] >= $levelup[8]['number']){
                    $upda['level'] = 9;
                    Db::name('user')->where(['id'=>$info['id']])->update($upda);
                    //升省代时  将自己添加到上级的升级条件中  上级的等级大于等于省代等级
                    $upinfo = UserInfo(['id'=>$info['recommend']]);
                    if($upinfo['level'] >= 9){
                        $heigh['shengdai'] = $upinfo['shengdai'] + 1;
                        Db::name('user')->where(['id'=>$upinfo['id']])->update($heigh);
                    }
                }
            }
            //省代->一星董事
            if($info['level'] == 9){
                if($info['shengdai'] >= $levelup[9]['number']){
                    $upda['level'] = 10;
                    Db::name('user')->where(['id'=>$info['id']])->update($upda);
                }
            }
            //一星董事->二星董事
            if($info['level'] == 10){
                if($info['shengdai'] >= $levelup[10]['number']){
                    $upda['level'] = 11;
                    Db::name('user')->where(['id'=>$info['id']])->update($upda);
                }
            }
            //二星董事->三星董事
            if($info['level'] == 11){
                if($info['shengdai'] >= $levelup[11]['number']){
                    $upda['level'] = 12;
                    Db::name('user')->where(['id'=>$info['id']])->update($upda);
                }
            }
        }
    }
    
    //管理奖
    public function guanli($GoodInfo){
        //系统参数
        $Config = Config();
        // var_dump($Config['yu']);//余额
        // var_dump($Config['ji']);//积分
        //获取当前用户id
        $UserID = Session::get('user_id');
        $where['user_id'] = $UserID;
        $where['recommend_id'] = [['neq', 1],['neq', $UserID]];
        //管理奖  将所有上级id取出   自己和admin除外
        $ids= Db::name('user_recommend')->where($where)->column('recommend_id');
        // $ids = [149,151,152,153,154];
        //用来判断平级奖等级不能比上级低
        $lock = 3;
        //用来判断平级奖拿的次数  拿过的等级全放进去  3 4 5 6-12
        $ci = array();
        //累计级差奖励
        $cha = 0;
        //累计平级奖励
        $ping = 0;
        // var_dump($ids);die;
        foreach ($ids as $val){
            //获取信息
            $info = UserInfo(['id'=>$val]);
            //3 4 5 6-12 将6-12等级全设置成6   用来区分1-4星店主
            if($info['level'] >6){
                $le = 6;
            }else{
                $le = $info['level'];
            }
            //先判断在不在数组中  在数次数
            if(in_array($le,$ci)){
                $count = array_count_values($ci)[$le];
            }else{
                $count = 0;
            }
            //会员等级>=3 即起码一星店主      会员等级>=上级等级     出现次数=0   
            //第一次出现  级差奖
            if($le >= 3 && $le >= $lock && $count == 0){
                //一星店主
                if($le == 3){
                    $money = $GoodInfo['oneji'];//级差奖
                    $cha = $GoodInfo['oneji'];
                    $tj_money = $money*$Config['yu'];//推荐奖余额
                    $tj_money1 = $money*$Config['ji'];//推荐奖积分
                    $new['balance_4']=$info['balance_4']+$tj_money;  //推荐奖励余额
                    $new['balance_1']=$info['balance_1']+$tj_money1;  //推荐奖励积分
                    $new['person_achievement']=$info['person_achievement']+$money;  //个人收益
                    if($tj_money > 0){
                        FinanceAdd($info['id'], $UserID, $tj_money, 4, 11, '级差奖余额');
                    }
                    if($tj_money1 > 0){
                        FinanceAdd($info['id'], $UserID, $tj_money1, 1, 11, '级差奖积分');
                    }
                    
                    Db::name('user')->where(['id'=>$info['id']])->update($new);
                }
                if($le == 4){
                    if(in_array($le-1,$ci)){
                        if(array_count_values($ci)[$le-1] >= 2){
                            $ping = $ping;
                        }else{
                            $ping = 0;
                        }
                    }else{
                        $ping = 0;
                    }
                    // $money = $GoodInfo['twoji'] - $cha - $ping;//级差奖
                    $money = $GoodInfo['twoji'] - $cha;//级差奖
                    $cha = $GoodInfo['twoji'];
                    $tj_money = $money*$Config['yu'];//推荐奖余额
                    $tj_money1 = $money*$Config['ji'];//推荐奖积分
                    $new['balance_4']=$info['balance_4']+$tj_money;  //推荐奖励余额
                    $new['balance_1']=$info['balance_1']+$tj_money1;  //推荐奖励积分
                    $new['person_achievement']=$info['person_achievement']+$money;  //个人收益
                    if($tj_money > 0){
                        FinanceAdd($info['id'], $UserID, $tj_money, 4, 11, '级差奖余额');
                    }
                    if($tj_money1 > 0){
                        FinanceAdd($info['id'], $UserID, $tj_money1, 1, 11, '级差奖积分');
                    }
                    
                    Db::name('user')->where(['id'=>$info['id']])->update($new);
                }
                if($le == 5){
                    if(in_array($le-1,$ci)){
                        if(array_count_values($ci)[$le-1] >= 2){
                            $ping = $ping;
                        }else{
                            $ping = 0;
                        }
                    }else{
                        $ping = 0;
                    }
                    // $money = $GoodInfo['threeji'] - $cha - $ping;//级差奖
                    $money = $GoodInfo['threeji'] - $cha;//级差奖
                    $cha = $GoodInfo['threeji'];
                    $tj_money = $money*$Config['yu'];//推荐奖余额
                    $tj_money1 = $money*$Config['ji'];//推荐奖积分
                    $new['balance_4']=$info['balance_4']+$tj_money;  //推荐奖励余额
                    $new['balance_1']=$info['balance_1']+$tj_money1;  //推荐奖励积分
                    $new['person_achievement']=$info['person_achievement']+$money;  //个人收益
                    if($tj_money > 0){
                        FinanceAdd($info['id'], $UserID, $tj_money, 4, 11, '级差奖余额');
                    }
                    if($tj_money1 > 0){
                        FinanceAdd($info['id'], $UserID, $tj_money1, 1, 11, '级差奖积分');
                    }
                    
                    Db::name('user')->where(['id'=>$info['id']])->update($new);
                }
                if($le == 6){
                    if(in_array($le-1,$ci)){
                        if(array_count_values($ci)[$le-1] >= 2){
                            $ping = $ping;
                        }else{
                            $ping = 0;
                        }
                    }else{
                        $ping = 0;
                    }
                    // $money = $GoodInfo['fourji'] - $cha - $ping;//级差奖
                    $money = $GoodInfo['fourji'] - $cha;//级差奖
                    $cha = $GoodInfo['fourji'];
                    $tj_money = $money*$Config['yu'];//推荐奖余额
                    $tj_money1 = $money*$Config['ji'];//推荐奖积分
                    $new['balance_4']=$info['balance_4']+$tj_money;  //推荐奖励余额
                    $new['balance_1']=$info['balance_1']+$tj_money1;  //推荐奖励积分
                    $new['person_achievement']=$info['person_achievement']+$money;  //个人收益
                    if($tj_money > 0){
                        FinanceAdd($info['id'], $UserID, $tj_money, 4, 11, '级差奖余额');
                    }
                    if($tj_money1 > 0){
                        FinanceAdd($info['id'], $UserID, $tj_money1, 1, 11, '级差奖积分');
                    }
                    
                    Db::name('user')->where(['id'=>$info['id']])->update($new);
                }
                //将等级设置为当前等级
                $lock = $le;
                //将当前等级放入数组中,用来判断该等级出现次数
                array_push($ci,$le);
            }
            //会员等级>=3 即起码一星店主      会员等级>=上级等级     出现次数=1   
            //第二次出现  平级奖
            if($le >= 3 && $le >= $lock && $count == 1){
                //一星店主
                if($le == 3){
                    $money = $GoodInfo['onep'];//平级奖
                    $ping = $GoodInfo['onep'];
                    $tj_money = $money*$Config['yu'];//推荐奖余额
                    $tj_money1 = $money*$Config['ji'];//推荐奖积分
                    $new['balance_4']=$info['balance_4']+$tj_money;  //推荐奖励余额
                    $new['balance_1']=$info['balance_1']+$tj_money1;  //推荐奖励积分
                    $new['person_achievement']=$info['person_achievement']+$money;  //个人收益
                    if($tj_money > 0){
                        FinanceAdd($info['id'], $UserID, $tj_money, 4, 11, '平级奖余额');
                    }
                    if($tj_money1 > 0){
                         FinanceAdd($info['id'], $UserID, $tj_money1, 1, 11, '平级奖积分');
                    }
                   
                    Db::name('user')->where(['id'=>$info['id']])->update($new);
                }
                if($le == 4){
                    $money = $GoodInfo['twop'];//平级奖
                    $ping = $GoodInfo['twop'];
                    $tj_money = $money*$Config['yu'];//推荐奖余额
                    $tj_money1 = $money*$Config['ji'];//推荐奖积分
                    $new['balance_4']=$info['balance_4']+$tj_money;  //推荐奖励余额
                    $new['balance_1']=$info['balance_1']+$tj_money1;  //推荐奖励积分
                    $new['person_achievement']=$info['person_achievement']+$money;  //个人收益
                    if($tj_money > 0){
                        FinanceAdd($info['id'], $UserID, $tj_money, 4, 11, '平级奖余额');
                    }
                    if($tj_money1 > 0){
                        FinanceAdd($info['id'], $UserID, $tj_money1, 1, 11, '平级奖积分');
                    }
                    
                    Db::name('user')->where(['id'=>$info['id']])->update($new);
                }
                if($le == 5){
                    $money = $GoodInfo['threep'];//平级奖
                    $ping = $GoodInfo['threep'];
                    $tj_money = $money*$Config['yu'];//推荐奖余额
                    $tj_money1 = $money*$Config['ji'];//推荐奖积分
                    $new['balance_4']=$info['balance_4']+$tj_money;  //推荐奖励余额
                    $new['balance_1']=$info['balance_1']+$tj_money1;  //推荐奖励积分
                    $new['person_achievement']=$info['person_achievement']+$money;  //个人收益
                    if($tj_money > 0){
                        FinanceAdd($info['id'], $UserID, $tj_money, 4, 11, '平级奖余额');
                    }
                    if($tj_money1 > 0){
                        FinanceAdd($info['id'], $UserID, $tj_money1, 1, 11, '平级奖积分');
                    }
                    
                    
                    Db::name('user')->where(['id'=>$info['id']])->update($new);
                }
                if($le == 6){
                    $money = $GoodInfo['fourp'];//平级奖
                    $ping = $GoodInfo['fourp'];
                    $tj_money = $money*$Config['yu'];//推荐奖余额
                    $tj_money1 = $money*$Config['ji'];//推荐奖积分
                    $new['balance_4']=$info['balance_4']+$tj_money;  //推荐奖励余额
                    $new['balance_1']=$info['balance_1']+$tj_money1;  //推荐奖励积分
                    $new['person_achievement']=$info['person_achievement']+$money;  //个人收益
                    if($tj_money > 0){
                        FinanceAdd($info['id'], $UserID, $tj_money, 4, 11, '平级奖余额');
                    }
                    if($tj_money1 > 0){
                        FinanceAdd($info['id'], $UserID, $tj_money1, 1, 11, '平级奖积分');
                    }
                    
                    
                    Db::name('user')->where(['id'=>$info['id']])->update($new);
                }
                //将等级设置为当前等级
                $lock = $le;
                //将当前等级放入数组中,用来判断该等级出现次数
                array_push($ci,$le);
            }
        }
    }
    
    // //区域奖
    // public function quyu($OrderInfo,$GoodInfo){
    //     //系统参数
    //     $Config = Config();
    //     //订单地址 天津 河北区 全境123  
    //     //用空格分割 然后惊醒判断
    //     $address = explode(' ',$OrderInfo['address']);
    //     //获取当前用户id
    //     $UserID = Session::get('user_id');
    //     // $where['user_id'] = $UserID;
    //     // $where['recommend_id'] = [['neq', 1],['neq', $UserID]];
    //     //管理奖  将所有上级id取出   自己和admin除外
    //     // $ids= Db::name('user_recommend')->where($where)->column('recommend_id');
    //     // $ids = [2];
    //     //用来判断级差不能比上级低
    //     $lock = 4;
    //     //累计级差奖励
    //     $cha = 0;
    //     $ids = Db::name('user')->where(['level'=>['gt',3]])->select();
    //     if($ids){
    //         foreach ($ids as $value){
    //             //获取信息
    //             // $info = UserInfo(['id'=>$value]);
    //             //查询地址信息
    //             // $infoaddress = Db::name('user_address')->where(['user_id'=>$value['id'],'is_default'=>1])->find()['area'];
    //             $infoaddress = $value['quyu'];
    //             //有地址再进行判断  并且等级>=$lock
    //             if($infoaddress && $value['level'] >= $lock){
    //                 //县代
    //                 if($value['level'] == 4){
    //                     //分割改地址
    //                     // $infoaddre = explode(' ',$infoaddress);
    //                     $infoaddre = $infoaddress;
    //                     //判断两个地址的最后一个是否相同 县有详细地址  只能看截取$address  $infoaddre
    //                     // $pd = strstr($address[2], $infoaddre[2]);
    //                     $pd = strstr($address[2], $infoaddre);
    //                     //如果有  说明是的
    //                     if($pd){
    //                         $money = $GoodInfo['xian']*$OrderInfo['num'];//级差奖
    //                         $cha = $GoodInfo['xian']*$OrderInfo['num'];//级差奖
    //                         // $tj_money = $money*$Config['yu'];//余额
    //                         // $tj_money1 = $money*$Config['ji'];//积分
    //                         $tj_money1 = $money;//积分
    //                         // $new['balance_4']=$value['balance_4']+$tj_money;  //推荐奖励余额
    //                         $new['balance_4']=$value['balance_4']+$tj_money1;  //推荐奖励积分
    //                         $new['person_achievement']=$value['person_achievement']+$money;  //个人收益
    //                         // FinanceAdd($value['id'], $UserID, $tj_money, 4, 11, '代理奖余额');
    //                         FinanceAdd($value['id'], $UserID, $tj_money1, 4, 11, '区县代奖励');
    //                         Db::name('user')->where(['id'=>$value['id']])->update($new);
    //                         $lock = $value['level'] + 1;
    //                     }
    //                 }
    //                 //市代
    //                 if($value['level'] == 5){
    //                     //分割改地址
    //                     // $infoaddre = explode(' ',$infoaddress);
    //                     $infoaddre = $infoaddress;
    //                     //判断两个地址的中间一个是否相同 
    //                     // if($address[1] == $infoaddre[1]){
    //                     if($address[1] == $infoaddre){
    //                         $money = $GoodInfo['shi']*$OrderInfo['num'] - $cha;//级差奖
    //                         $cha = $GoodInfo['shi']*$OrderInfo['num'];//级差奖
    //                         // $tj_money = $money*$Config['yu'];//余额
    //                         // $tj_money1 = $money*$Config['ji'];//积分
    //                         $tj_money1 = $money;//积分
    //                         // $new['balance_4']=$value['balance_4']+$tj_money;  //推荐奖励余额
    //                         $new['balance_4']=$value['balance_4']+$tj_money1;  //推荐奖励积分
    //                         $new['person_achievement']=$value['person_achievement']+$money;  //个人收益
    //                         // FinanceAdd($value['id'], $UserID, $tj_money, 4, 11, '代理奖余额');
    //                         FinanceAdd($value['id'], $UserID, $tj_money1, 4, 11, '市代奖励');
    //                         Db::name('user')->where(['id'=>$value['id']])->update($new);
    //                         $lock = $value['level'] + 1;
    //                     }
    //                 }
    //                 //省代
    //                 if($value['level'] == 6){
    //                     //分割改地址
    //                     // $infoaddre = explode(' ',$infoaddress);
    //                     $infoaddre = $infoaddress;
    //                     //判断两个地址的中间一个是否相同 
    //                     // if($address[0] == $infoaddre[0]){
    //                     if($address[0] == $infoaddre){
    //                         $money = $GoodInfo['sheng']*$OrderInfo['num'] - $cha;//级差奖
    //                         $cha = $GoodInfo['sheng']*$OrderInfo['num'];//级差奖
    //                         // $tj_money = $money*$Config['yu'];//余额
    //                         // $tj_money1 = $money*$Config['ji'];//积分
    //                         $tj_money1 = $money;//积分
    //                         // $new['balance_4']=$value['balance_4']+$tj_money;  //推荐奖励余额
    //                         $new['balance_4']=$value['balance_4']+$tj_money1;  //推荐奖励积分
    //                         $new['person_achievement']=$value['person_achievement']+$money;  //个人收益
    //                         // FinanceAdd($value['id'], $UserID, $tj_money, 4, 11, '代理奖余额');
    //                         FinanceAdd($value['id'], $UserID, $tj_money1, 4, 11, '省代奖励');
    //                         Db::name('user')->where(['id'=>$value['id']])->update($new);
    //                         $lock = $value['level'] + 1;
    //                     }
    //                 }
    //             }
                
    //         }
    //     }
        
    // }
    
    //区域奖
    public function quyu($OrderInfo,$GoodInfo,$numb){
        //系统参数
        $Config = Config();
        //订单地址 天津 河北区 全境123  
        //用空格分割 然后惊醒判断
        $address = explode(' ',$OrderInfo['address']);
        //获取当前用户id
        $UserID = Session::get('user_id');
        // $where['user_id'] = $UserID;
        // $where['recommend_id'] = [['neq', 1],['neq', $UserID]];
        //管理奖  将所有上级id取出   自己和admin除外
        // $ids= Db::name('user_recommend')->where($where)->column('recommend_id');
        // $ids = [2];
        //用来判断级差不能比上级低
        $lock = 1;
        //累计级差奖励
        $cha = 0;
        $ids = Db::name('user')->where(['level_1'=>['gt',0],'id'=>['neq',$UserID]])->select();
        if($ids){
            //先算区县代
            foreach ($ids as $value){
                $infoaddress = $value['quyu'];
                //有地址再进行判断  并且等级>=$lock
                if($infoaddress && $value['level_1'] >= $lock){
                    //县代
                    if($value['level_1'] == 1){
                        //分割改地址
                        // $infoaddre = explode(' ',$infoaddress);
                        $infoaddre = $infoaddress;
                        //判断两个地址的最后一个是否相同 县有详细地址  只能看截取$address  $infoaddre
                        // $pd = strstr($address[2], $infoaddre[2]);
                        $pd = strstr($address[2], $infoaddre);
                        //如果有  说明是的
                        if($pd){
                            @$money = $GoodInfo['xian']*$numb;//级差奖
                            @$cha = $GoodInfo['xian']*$numb;//级差奖
                            // $tj_money = $money*$Config['yu'];//余额
                            // $tj_money1 = $money*$Config['ji'];//积分
                            $tj_money1 = $money;//积分
                            // $new['balance_4']=$value['balance_4']+$tj_money;  //推荐奖励余额
                            $new['balance_4']=$value['balance_4']+$tj_money1;  //推荐奖励积分
                            $new['person_achievement']=$value['person_achievement']+$money;  //个人收益
                            // FinanceAdd($value['id'], $UserID, $tj_money, 4, 11, '代理奖余额');
                            if($tj_money1 > 0){
                                 FinanceAdd($value['id'], $UserID, $tj_money1, 4, 11, '区县代奖励',$OrderInfo['order_id']);
                            }
                           
                            Db::name('user')->where(['id'=>$value['id']])->update($new);
                            $lock = $value['level_1'] + 1;
                        }
                    }
                }
                
            }
            //市代
            foreach ($ids as $value){
                $infoaddress = $value['quyu'];
                //有地址再进行判断  并且等级>=$lock
                if($infoaddress && $value['level_1'] >= $lock){
                    //市代
                    if($value['level_1'] == 2){
                        //分割改地址
                        // $infoaddre = explode(' ',$infoaddress);
                        $infoaddre = $infoaddress;
                        //判断两个地址的中间一个是否相同 
                        // if($address[1] == $infoaddre[1]){
                        if($address[1] == $infoaddre){
                            @$money = $GoodInfo['shi']*$numb - $cha;//级差奖
                            @$cha = $GoodInfo['shi']*$numb;//级差奖
                            // $tj_money = $money*$Config['yu'];//余额
                            // $tj_money1 = $money*$Config['ji'];//积分
                            $tj_money1 = $money;//积分
                            // $new['balance_4']=$value['balance_4']+$tj_money;  //推荐奖励余额
                            $new['balance_4']=$value['balance_4']+$tj_money1;  //推荐奖励积分
                            $new['person_achievement']=$value['person_achievement']+$money;  //个人收益
                            // FinanceAdd($value['id'], $UserID, $tj_money, 4, 11, '代理奖余额');
                            if($tj_money1 > 0){
                                FinanceAdd($value['id'], $UserID, $tj_money1, 4, 11, '市代奖励',$OrderInfo['order_id']);
                            }
                            
                            Db::name('user')->where(['id'=>$value['id']])->update($new);
                            $lock = $value['level_1'] + 1;
                        }
                    }
                    
                }
                
            }
            //省代
            foreach ($ids as $value){
                $infoaddress = $value['quyu'];
                //有地址再进行判断  并且等级>=$lock
                if($infoaddress && $value['level_1'] >= $lock){
                    //省代
                    if($value['level_1'] == 3){
                        //分割改地址
                        // $infoaddre = explode(' ',$infoaddress);
                        $infoaddre = $infoaddress;
                        //判断两个地址的中间一个是否相同 
                        // if($address[0] == $infoaddre[0]){
                        if($address[0] == $infoaddre){
                            @$money = $GoodInfo['sheng']*$numb - $cha;//级差奖
                            @$cha = $GoodInfo['sheng']*$numb;//级差奖
                            // $tj_money = $money*$Config['yu'];//余额
                            // $tj_money1 = $money*$Config['ji'];//积分
                            $tj_money1 = $money;//积分
                            // $new['balance_4']=$value['balance_4']+$tj_money;  //推荐奖励余额
                            $new['balance_4']=$value['balance_4']+$tj_money1;  //推荐奖励积分
                            $new['person_achievement']=$value['person_achievement']+$money;  //个人收益
                            // FinanceAdd($value['id'], $UserID, $tj_money, 4, 11, '代理奖余额');
                            if($tj_money1 > 0){
                                FinanceAdd($value['id'], $UserID, $tj_money1, 4, 11, '省代奖励',$OrderInfo['order_id']);
                            }
                            
                            Db::name('user')->where(['id'=>$value['id']])->update($new);
                            $lock = $value['level_1'] + 1;
                        }
                    }
                }
                
            }
        }
        
    }
    
    //参团人
    public function tuan($OrderInfo){
        
        $pt = Db::name('user_pt')->where(['pt_id'=>$OrderInfo['pt_id']])->find();
        //获取当前用户id
        $UserID = Session::get('user_id');
        //参团人数+1  参团人拼接进去
        $ptdata['can_people'] = $pt['can_people'] + 1;
        if($pt['can']){
            $ptdata['can'] = $pt['can'] .','.$UserID;
        }else{
            $ptdata['can'] = $UserID;
        }
        
        if($ptdata['can_people'] >= $pt['pt_people']){
            $ptdata['status'] = 2;
        }
        Db::name('user_pt')->where(['id'=>$pt['id']])->update($ptdata);
    }
    
    //开奖
    public function kai($OrderInfo,$GoodInfo){
    // public function kai(){
        $pt = Db::name('user_pt')->where(['pt_id'=>$OrderInfo['pt_id']])->find();
        // $pt = Db::name('user_pt')->where(['pt_id'=>20210401145713706])->find();
        //2准备开奖   
        if($pt['status'] == 2){
            //参加的人
            $peoples = explode(',',$pt['can']);
            //将团长移除  不开奖
            $key=array_search($pt['chuang'] ,$peoples);
            array_splice($peoples,$key,1);
            //中奖的人  随机取   array_rand是取键   所以array_flip  键值交换下
            $zhong = array_rand(array_flip($peoples),$pt['get_people']);
            //循环处理所有拼团的人
            foreach ($peoples as $v){
                //获取参团人信息
                $info = Db::name('user')->where(['id'=>$v])->find();
                //获取参团人订单信息
                $order = Db::name('user_order')->where(['pt_id'=>$pt['pt_id'],'user_id'=>$v])->find();
                //商品信息
                $GoodInfo = Db::name('goods')->where(['id'=>$order['goods_id'] ])->find();
                //处理中奖
                if(in_array($v,$zhong)){
                    //获取购买金额
                    if($v == $pt['chuang']){
                        $money = $pt['quan']*$order['num'];
                    }else{
                        $money = $pt['price']*$order['num'];
                    }
                    
                    if($info){
                        //将释放积分存入user表
                        $sf['sfjifen'] = $info['sfjifen'] + $money;
                        Db::name('user')->where(['id'=>$v])->update($sf);
                        
                        //修改订单状态为5   成功
                        $success['status'] = 5;
                        Db::name('user_order')->where(['id'=>$order['id']])->update($success);
                        //团队业绩
                        Db::query('call add_recommend_num(:id,:num )',['id'=>$v,'num'=>$money]);    // team_num
                        //直推奖
                        $higherInfo= Db::name('user')->where(['id'=>$info['recommend']])->find();
                        if($higherInfo){
                            $tj_money = $GoodInfo['reward_1']*$order['num'];//推荐奖
                            $tj_money1 = $GoodInfo['zhiyu']*$order['num'];//推荐奖
                            //店铺级以上  直推奖励到余额    其他积分
                            if($higherInfo['level'] >= 3){
                                $recommend['balance_4']=$higherInfo['balance_4']+$tj_money1;  //推荐奖励余额
                                $recommend['balance_1']=$higherInfo['balance_1'];
                                // $recommend['person_achievement']=$higherInfo['person_achievement']+$tj_money;  //个人收益
                                $ms = '推荐奖余额';
                                if($tj_money1 > 0){
                                    FinanceAdd($info['recommend'], $info['id'], $tj_money1, 4, 11, $ms,$OrderInfo['order_id']);
                                }
                                
                            }else{
                                $recommend['balance_1']=$higherInfo['balance_1']+$tj_money;  //推荐奖励积分
                                $recommend['balance_4']=$higherInfo['balance_4'];
                                // $recommend['person_achievement']=$higherInfo['person_achievement']+$tj_money;  //个人收益
                                $ms = '推荐奖积分';
                                if($tj_money > 0){
                                    FinanceAdd($info['recommend'], $info['id'], $tj_money, 1, 11, $ms,$OrderInfo['order_id']);
                                }
                                
                            }
                            $recommend['person_achievement']=$higherInfo['person_achievement']+$tj_money;  //个人收益
                            Db::name('user')->where(['id'=>$info['recommend']])->update($recommend);
                            // FinanceAdd($info['recommend'], $info['id'], $tj_money, 4, 11, $ms);
                        }
                        
                        // //区域奖
                        $this->quyu($order,$GoodInfo);
                    }
                }else{
                    //未中奖
                    //获取购买金额
                    if($v == $pt['chuang']){
                        $money = $pt['quan']*$order['num'];
                    }else{
                        $money = $pt['price']*$order['num'];
                    }
                    if($info){
                        //修改订单状态为6  失败
                        $false['status'] = 11;
                        Db::name('user_order')->where(['id'=>$order['id']])->update($false);
                        // //余额  =  原余额 + 退还 +  红包
                        // $back['balance_4'] = $info['balance_4'] + $money + $pt['hongbao'];
                        // FinanceAdd($info['id'], $info['id'], $money, 4, 11, '拼团退款');
                        // Db::name('user')->where(['id'=>$info['id']])->update($back);
                        //余额  =  原余额 + 退还 +  红包
                        // if($OrderInfo['pay_type'] == 9){
                        //     wxrefund($out_trade_no,$OrderInfo['amount']*100,$OrderInfo['amount']*100);
                        //     FinanceAdd($info['id'], $info['id'], $OrderInfo['amount'], 9, 11, '拼团退款');
                        // }else{
                        //     $back['balance_4'] = $info['balance_4'] + $money + $pt['hongbao'];
                        //     FinanceAdd($info['id'], $info['id'], $money, 4, 11, '拼团退款');
                        //     Db::name('user')->where(['id'=>$info['id']])->update($back);
                        // }
                        // //修改订单状态为6  失败
                        // $false['status'] = 6;
                        // Db::name('user_order')->where(['id'=>$order['id']])->update($false);
                    }
                }
            }
            //存入中奖人  修改状态3
                $zj['zhong'] = implode(',',$zhong);
                $zj['status'] = 3;
                Db::name('user_pt')->where(['id'=>$pt['id']])->update($zj);
        }
        // var_dump($order);die;
    }
    
    //店铺额外奖励
    public function dian($GoodInfo){
        //用户信息
        $UserInfo = UserInfo(['id'=>Session::get('user_id')]);
        $higherInfo= Db::name('user')->where(['id'=>$UserInfo['recommend']])->find();
        if($higherInfo['level'] == 3 ){
            if($GoodInfo['dian'] != null){
                $dian['balance_4']=$higherInfo['balance_4'] + $GoodInfo['dian'];  //店铺奖励余额
                if($GoodInfo['dian'] > 0){
                    FinanceAdd($higherInfo['id'], $UserInfo['id'], $GoodInfo['dian'], 4, 11, '店铺奖励');
                }
                
                Db::name('user')->where(['id'=>$higherInfo['id']])->update($dian);
            }
        }else{
            //获取当前用户id
            $UserID = Session::get('user_id');
            $where['user_id'] = $UserID;
            $where['recommend_id'] = [['neq', 1],['neq', $UserID]];
            //升级时  将所有上级id取出   自己和admin除外
            $ids= Db::name('user_recommend')->where($where)->column('recommend_id');
            if($ids){
                //遍历处理
                foreach ($ids as $v){
                    //获取信息
                    $info = UserInfo(['id'=>$v]);
                    if($info['level'] == 3){
                        if($GoodInfo['dian'] != null){
                            $dian['balance_4']=$info['balance_4'] + $GoodInfo['dian'];  //店铺奖励余额
                            if($GoodInfo['dian'] > 0){
                                FinanceAdd($info['id'], $UserInfo['id'], $GoodInfo['dian'], 4, 11, '店铺奖励');
                            }
                            
                            Db::name('user')->where(['id'=>$info['id']])->update($dian);
                            break;
                        }
                        
                    }
                }
            }
        }
    }
    
    
    
    
}